package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;

/**
 * Servlet implementation class Apply
 */
@WebServlet("/Apply")
public class Apply extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Apply() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		String jobid = request.getParameter("job");
		String provider = request.getParameter("provider");
		String employee= session.getAttribute("user").toString();
		String state = null;
		
		System.out.println(jobid+ provider);
		
		Connection con = DBHelper.getConnection();
		
		ResultSet rs;
		
		PreparedStatement sql1 = DBHelper.getprepareStatement(con,"select jobid from applyjob where jobid = ?");
		try {
			sql1.setString(1, jobid);
			 rs= sql1.executeQuery();
		
		
		if(rs.next()){
			//do nothing
		}else{
			PreparedStatement sql = DBHelper.getprepareStatement(con,"insert into applyjob values(?,?,?,?)");
			try {
				sql.setString(1, jobid);
				sql.setString(2, employee);
				sql.setString(3, provider);
				sql.setString(4, state);
				sql.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}
